package tmhprediction.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;

import tmhprediction.classification.TMHClassifier;
import tmhprediction.eval.TMHHelixEvaluator;
import tmhprediction.eval.TMHResidueEvaluator;
import tmhprediction.eval.WriteOutput;

/***
 * Builds all models
 * based on the following directory structure:
 * + root directory
 * 		+ firstClassifier
 * 		+ secondClassifier
 * 		+ impSetOut
 * 		+ impSolSetOut
 * @author michi
 */

public class BuildModels {
	
	private static String fileRootDirectory;
	private static String dirForFirstClassifier;
	private static String dirForSecondClassifier;
	private static String dirimpSolSetOut;
	private static String dirimpSetOut;
	
	private static double parameter_c = 0.5;
	private static double parameter_g = 0.0078;
	
	public static void main(String[] args) throws Exception {
		System.out.println("Please enter root directory path: ");
		
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		fileRootDirectory = bufferedReader.readLine();
		
//		if (args.length > 0) {
//		    try {
//		        fileRootDirectory = args[0];
//		    } catch (Exception e) {
//		        System.err.println("While reading form commandline...");
//		        System.exit(1);
//		    }
//		}
		
		dirForFirstClassifier = fileRootDirectory+"/firstClassifier";
		dirForSecondClassifier = fileRootDirectory+"/secondClassifier";
		dirimpSolSetOut = fileRootDirectory+"/impSolSetOut/features_manual_";
		dirimpSetOut = fileRootDirectory+"/impSolSetOut/features_manual_";
        
        //build models for classifier 1 - impsol
        buildModelsforClassifier(dirimpSolSetOut, dirForFirstClassifier);

        //build models for classifier 2 - sol
        buildModelsforClassifier(dirimpSetOut, dirForSecondClassifier);
	}
	
	static void buildModelsforClassifier(String pathDataSets, String pathtoClassifier) throws Exception
	{
		String TrainingSet = null;
        String TestingSet = null;
        String OutputFolder = null;
        String ModelIndex = null;
        
        //build models for classifier 1 - impsol
        for(int i = 0; i < 3; i++)
        {
        	OutputFolder = pathtoClassifier;
        	
        	switch(i) {
        	case 0:	TrainingSet = pathDataSets+"0.arff";
    				TestingSet  = pathDataSets+"1.arff";
    				ModelIndex  = "0";
    				break;
        		
        	case 1:	TrainingSet = pathDataSets+"1.arff";
					TestingSet  = pathDataSets+"2.arff";
					ModelIndex  = "1";
					break;
        		
        	case 2: TrainingSet = pathDataSets+"2.arff";
					TestingSet  = pathDataSets+"0.arff";
					ModelIndex  = "2";
					break;
        	}
        	try {
    		doIt(TrainingSet, TestingSet, OutputFolder, ModelIndex);
        	}
        	catch(Exception e) {
		        System.err.println("buildModelsforClassifier...");
		        System.exit(1);
        	}
        }	
	}
	
	static void doIt(String trainingSet, String testingSet, String outFolder, String index) throws Exception
	{
		double c = parameter_c;
		double g = parameter_g;
		
		System.out.println("Start");
		TMHClassifier classifier = new TMHClassifier(trainingSet, testingSet, g,c ,40);
		System.out.println("Running with C:"+classifier.getcost() +" and gamma:"+classifier.getGamma() + ". Train and test are: "+trainingSet+" "+testingSet);
		
		System.out.println("Training done, now classification and evaluation");
		HashMap<String, double[]> resM = classifier.createResultMap();
		
		System.out.println("Calculate per residue scores");
		TMHResidueEvaluator.calcPerResidueScores(resM, outFolder+"/"+index+"_scoresPerRes");
		
		System.out.println("Writing out predictions in human readable format");
//		WriteOutput.writeObservedAndPredicted(resM, outFolder+"/"+index+"_readablePrediction");
		
		System.out.println("Calculating and writing out per segment scores");
//		TMHHelixEvaluator.berechneFormeln(TMHHelixEvaluator.finalHelixEvaluation(resM), outFolder+"/"+index+"_scoresPerSeg");

		classifier.saveModel(outFolder+"/"+index+"_model.svm");
		System.out.println("Model saved (for realz!)");
	}
	
}

